;; The first four lines of this file were added by Dracula.
;; They tell DrScheme that this is a Dracula Modular ACL2 program.
;; Leave these lines unchanged so that DrScheme can properly load this file.
#reader(planet "reader.rkt" ("cce" "dracula.plt") "modular" "lang")
(require "Iperformance.lisp")
(require "Istockhelper.lisp")

(module Mperformance

(import Istockhelper)

#| -------------------------------------------|#  
#| *** functions to compute performance query ***
|#
;; get performance of a specific ticker
; format (Ticker, performance)
(defun performance (ticker start-date end-date ticker-list flat-data-avl)
  (let* ((ticker-data (get-ticker-data ticker ticker-list flat-data-avl))
         (first-date-key (get-first-date start-date ticker-data))
         (last-date-key (get-last-date end-date ticker-data)))
    (if (and (not (equal first-date-key nil))
             (not (equal last-date-key nil))
             (<= (second first-date-key) (second last-date-key)))
         (let* ((first-date-data (get-date-data first-date-key ticker-data))
                (first-closing-price (fourth (cdr first-date-data)))
                (last-date-data (get-date-data last-date-key ticker-data))
                (last-closing-price (fourth (cdr last-date-data))))
           (if (= first-closing-price last-closing-price)
               (list (list ticker) 0)
               (list (list ticker) (* (/ (- last-closing-price first-closing-price) first-closing-price) 100))))
         (list (list ticker) -100))))

;; helps to get the highest performance for the performance query
(defun performance-query-helper (current-highest-performance start-date end-date query-tickers ticker-list flat-data-avl)
  (if (consp query-tickers)
      (performance-query-helper (max-performance/volatility current-highest-performance
                                                            (performance (car query-tickers) start-date end-date ticker-list flat-data-avl))
                                start-date
                                end-date
                                (cdr query-tickers)
                                ticker-list
                                flat-data-avl)
      current-highest-performance))

;; computes performance query
(defun performance-query (start-date end-date query-tickers ticker-list flat-data-avl)
  (performance-query-helper '("" -100) start-date end-date query-tickers ticker-list flat-data-avl))
#| -------------------------------------------|#

(export Iperformance))